﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using WuDada.Core.SystemApplications.Domain;

public partial class admin_UC05_0501 : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IMessageService m_MessageService;
    private WebLogService m_WebLogService;

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_MessageService = m_PostFactory.GetMessageService();
        m_WebLogService = new WebLogService();

        if (!Page.IsPostBack)
        {
            pnlContent.Visible = false;                        
            fillGridView();
            ShowMode();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
        }
    }

    protected void btnQuery_Click(object sender, ImageClickEventArgs e)
    {
        fillGridView();
    }

    private void fillGridView()
    {
        //搜尋條件
        DateTime? startDate = ConvertUtil.ToDateTimeMin(txtDateFrom.Text);
        DateTime? endDate = ConvertUtil.ToDateTimeMax(txtDateTo.Text);
        string sortField = "CreatedDate";
        bool sortDesc = true;

        //分頁
        AspNetPager1.RecordCount = m_MessageService.CountMessage(string.Empty, startDate, endDate);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        gvDetail.DataSource = m_MessageService.GetMessageList(string.Empty, startDate, endDate, pageIndex, pageSize, sortField, sortDesc);
        gvDetail.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int messageId = int.Parse(e.CommandArgument.ToString());
        MessageVO messageVO = m_MessageService.GetMessageById(messageId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                m_Mode = messageId;
                UIHelper.FillUI(pnlContent, messageVO);
                ckeContent.value = messageVO.Answer;
                ShowMode();
                pnlContent.Visible = true;
                break;
            default:
                break;
        }
        fillGridView();
    }


    protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            Control ctrl = e.Row;
            MessageVO messageVO = (MessageVO)e.Row.DataItem;
            if (!string.IsNullOrEmpty(messageVO.Answer))
            {
                UIHelper.SetLabelText(ref ctrl, "lblReply", "是");
            }
            if (messageVO.AdminAgreePublish)
            {
                UIHelper.SetLabelText(ref ctrl, "lblShow", "是");
            }
        }
    }

    private void ClearUI()
    {
        m_Mode = 0;
        UIHelper.ClearUI(pnlContent);
        pnlContent.Visible = false;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        MessageVO messageVO = m_MessageService.GetMessageById(m_Mode);
        UIHelper.FillVO(pnlContent, messageVO);
        messageVO.Answer = ckeContent.value;
        m_MessageService.UpdateMessage(messageVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.修改, messageVO);
        fillGridView();
        ClearUI();
        ShowMode();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
    }
}